﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DoeObjects;
using System.Collections.Generic;

namespace DoeLibsTest
{
	[TestClass]
	public class Topic_Test
	{
		[TestInitialize]
		public void initializeDatabaseConnection()
		{
			TestContext.initialize();
		}

		/// <summary>
		/// Tests the topic methods: save, update, add, getAll, getByTopicId.
		/// </summary>
		[TestMethod]
		public void test_save()
		{
			// add
			List<Topic> allTopics = Topic.getAll();
			Topic topic = Helper.createTopicObject();
			Assert.IsTrue(topic.save(),
				"Topic: save() returned false when adding topic.");
			Assert.IsTrue(allTopics.Count + 1 == Topic.getAll().Count,
				"Topic: Topic was not saved or getAll() is not working.");

			Topic addedTopic = Topic.getByTopicId(topic.TopicId);
			Assert.IsTrue((topic.TopicName == addedTopic.TopicName) &&
				(topic.TopicId == addedTopic.TopicId),
				"Topic: The added topic does not have same name or id.");

			//update
			allTopics = Topic.getAll();
			topic.TopicName = Helper.getRandomString();
			Assert.IsTrue(topic.save(),
				"Topic: save() returned false when updating topic.");
			Assert.IsTrue(allTopics.Count == Topic.getAll().Count,
				"Topic: Topic was probably added instead of updated.");

			allTopics = Topic.getAll();
			bool topicIsInListTest = false;
			foreach (Topic t in allTopics)
			{
				if (t.TopicId == topic.TopicId &&
					t.TopicName == topic.TopicName)
				{
					topicIsInListTest = true;
					break;
				}
			}
			Assert.IsTrue(topicIsInListTest, "Topic: getAll() does not work.");

			addedTopic = Topic.getByTopicId(topic.TopicId);
			Assert.IsTrue((topic.TopicName == addedTopic.TopicName) &&
				(topic.TopicId == addedTopic.TopicId),
				"Topic: The updated topic does not have same name or id.");
		}

		[TestMethod]
		public void test_getByName()
		{
			string searchableString = Helper.getRandomString().Substring(0, 10);
			Topic topic1 = new Topic();
			Topic topic2 = new Topic();
			topic1.TopicName = searchableString + Helper.getRandomString().Substring(11);
			topic2.TopicName = searchableString + Helper.getRandomString().Substring(11);
			topic1.save();
			topic2.save();

			List<Topic> topicByName = Topic.getByName(searchableString, true);
			Assert.IsTrue(topicByName.Count == 2, "Topic: getByName() with like expression did not fetch correct amount.");
			Assert.IsTrue(Topic.getByName(searchableString, false).Count == 0, "Topic: getByName() without like expression behave bad.");
			Assert.IsTrue(Topic.getByName(topic1.TopicName, false).Count == 1, "Topic: getByName() without like expression did not return exact match.");
		}

		/// <summary>
		/// Tests the topic methods: getTopicsFromBook, Title.addTopic, Title.getTitlesFromTopic.
		/// </summary>
		[TestMethod]
		public void test_getTopicsFromBook()
		{
			Topic topic1 = Helper.createTopicObject(true);
			Topic topic2 = Helper.createTopicObject(true);

			Title title = Helper.createTitle(true);
			Title.addTopic(topic1.TopicId, title.TitleId);
			Title.addTopic(topic2.TopicId, title.TitleId);

			List<Topic> topics = Topic.getTopicsFromBook(title.TitleId);
			Assert.IsTrue(topics.Count == 2, "Topic: The new title does not contain 2 topics as intended.");

			List<Title> titles = Title.getTitlesFromTopic(topic1.TopicId);
			Assert.IsTrue(titles.Count == 1, "Topic: Title.getTitlesFromTopic does not return correct amount.");
		}
	}
}
